package projectsupporter.form;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;

import projectsupporter.common.constant.Constant;
import projectsupporter.common.control.BannerComposite;
import projectsupporter.common.control.Path;
import projectsupporter.common.logic.GlobalInfo;
import projectsupporter.common.logic.PlugUILogic.ImagePath;
import projectsupporter.common.operation.CreateMasterOperation;
import projectsupporter.common.operation.LoadTableInfoOperation;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TreeItem;

import dbTools.DBToolEngine;
import dbTools.coeModel.CodeModelCreator;
import dbTools.factory.CodeModelFactory;
import dbTools.tableInfo.info.TableInfo;
import org.eclipse.swt.widgets.Button;

public class CreateMasterForm {

	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private Composite composite = null;
	private BannerComposite bannerComposite = null;
	private Composite mainComposite = null;
	private Table table = null;
	private Group group = null;
	private Shell parentShell;
	
	private Map<String,TableInfo> tableInfos;
	private Composite topComposite = null;
	private Composite botComposite = null;
	private Label label = null;
	private Combo combo = null;
	private Label label1 = null;
	private Button button = null;
	public CreateMasterForm(Shell parentShell,TreeItem[] items)
	{
		this.parentShell=parentShell;
		tableInfos=new LinkedHashMap<String,TableInfo>();
		createSShell();
		addSelectedItem(items);
	}
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		
		if(parentShell==null)
		{
			sShell = new Shell();
		}
		else
		{
			sShell = new Shell(parentShell, SWT.DIALOG_TRIM| SWT.APPLICATION_MODAL);
		}
		sShell.setText("CreateMaster");
		createComposite();
		sShell.setSize(new Point(654, 472));
		sShell.setLayout(new FillLayout());
	}

	/**
	 * This method initializes composite	
	 *
	 */
	private void createComposite() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 0;
		composite = new Composite(sShell, SWT.NONE);
		composite.setLayout(gridLayout);
		createBannerComposite();
		createMainComposite();
	}

	/**
	 * This method initializes bannerComposite	
	 *
	 */
	private void createBannerComposite() {
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 2;
		gridLayout1.marginWidth = 0;
		gridLayout1.verticalSpacing = 0;
		gridLayout1.horizontalSpacing = 0;
		gridLayout1.marginHeight = 0;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.heightHint = 67;
		gridData.grabExcessHorizontalSpace = true;
		Image bannerImage = new Image(sShell.getDisplay(),ImagePath.EXP_PRODUCT);
		bannerComposite = new BannerComposite(composite, SWT.NONE,bannerImage);
		bannerComposite.setLayoutData(gridData);
		bannerComposite.setLayout(gridLayout1);
		bannerComposite.setBanTitle("CreateMaster");
		bannerComposite.setBanInfo("Create master codes by CodeModel");
	}

	/**
	 * This method initializes mainComposite	
	 *
	 */
	private void createMainComposite() {
		GridLayout gridLayout2 = new GridLayout();
		gridLayout2.numColumns = 2;
		GridData gridData2 = new GridData();
		gridData2.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData2.widthHint = 100;
		gridData2.grabExcessVerticalSpace = true;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		mainComposite = new Composite(composite, SWT.NONE);
		mainComposite.setLayoutData(gridData1);
		mainComposite.setLayout(gridLayout2);
		table = new Table(mainComposite, SWT.MULTI | SWT.BORDER);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setBounds(new Rectangle(8, 6, 104, 380));
		table.setLayoutData(gridData2);
		table.setLinesVisible(true);
		createGroup();
		TableColumn col1 = new TableColumn(table, SWT.LEFT);
		col1.setText("TableName");
		col1.setWidth(table.getSize().x);
		TableColumn col2 = new TableColumn(table, SWT.LEFT);
		col2.setText("TableId");
		col2.setWidth(0);
		col2.setResizable(false);
	}

	/**
	 * This method initializes group	
	 *
	 */
	private void createGroup() {
		GridLayout gridLayout3 = new GridLayout();
		GridData gridData3 = new GridData();
		gridData3.grabExcessVerticalSpace = true;
		gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData3.grabExcessHorizontalSpace = true;
		group = new Group(mainComposite, SWT.NONE);
		group.setText("Model Info");
		group.setLayout(gridLayout3);
		createTopComposite();
		group.setLayoutData(gridData3);
		createBotComposite();
	}
	
	/**
	 * 打开
	 */
	public void open() {
		final Display display = Display.getDefault();
		sShell.open();// 打开文件夹选择窗口
		sShell.layout();
		while (!sShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
//		display.dispose();
		
		return ;
	}
	
	/**
	 * 关闭
	 */
	protected void close() {
		if (sShell != null)
			sShell.dispose();// 释放底层的资源
	}

	public void addSelectedItem(TreeItem[] items)
	{
		for(TreeItem item : items)
		{
			if(item.getData(Constant.ITEM_TYPE).equals(Constant.ITEM_TYPE_TABLE)==false)
			{
				continue;
			}
			
			TableInfo tableInfo=DBToolEngine.TABLE_INFO_MAP.get(item.getData(Constant.TABLE_KEY).toString());
			int itemIndex= getExistedItemIndex(tableInfo.getTableName());
			
			if(itemIndex==-1)
			{
				new TableItem(table, SWT.LEFT).setText(new String[] {tableInfo.getTableName(),tableInfo.getKey()});
				this.tableInfos.put(tableInfo.getKey(), tableInfo);
			}		
		}
		System.out.println("sus");
	}
	
	private int getExistedItemIndex(String tableName)
	{
		if(table.getItems()==null)
		{
			return -1;
		}
		
		for(int i= 0;i<table.getItemCount();i++)
		{
			TableItem item = table.getItem(i);
			if(item.getText(0).equals(tableName))
			{
				return i;
			}
		}
		
		return -1;
	}
	/**
	 * This method initializes topComposite	
	 *
	 */
	private void createTopComposite() {
		GridData gridData6 = new GridData();
		gridData6.widthHint = 100;
		GridData gridData4 = new GridData();
		gridData4.grabExcessHorizontalSpace = true;
		gridData4.heightHint = 200;
		gridData4.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData4.grabExcessVerticalSpace = true;
		gridData4.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridLayout gridLayout4 = new GridLayout();
		gridLayout4.numColumns = 2;
		topComposite = new Composite(group, SWT.NONE);
		topComposite.setLayout(gridLayout4);
		topComposite.setLayoutData(gridData4);
		label = new Label(topComposite, SWT.RIGHT);
		label.setText("Code Model:");
		label.setLayoutData(gridData6);
		createCombo();
	}
	/**
	 * This method initializes botComposite	
	 *
	 */
	private void createBotComposite() {
		GridData gridData9 = new GridData();
		gridData9.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
		gridData9.grabExcessVerticalSpace = true;
		gridData9.horizontalIndent = 30;
		gridData9.widthHint = 100;
		GridData gridData8 = new GridData();
		gridData8.grabExcessHorizontalSpace = true;
		gridData8.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData5 = new GridData();
		gridData5.heightHint = 80;
		gridData5.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData5.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		gridData5.grabExcessHorizontalSpace = true;
		botComposite = new Composite(group, SWT.NONE);
		botComposite.setLayout(new GridLayout());
		botComposite.setLayoutData(gridData5);
		label1 = new Label(botComposite, SWT.HORIZONTAL | SWT.SEPARATOR);
		label1.setText("Label");
		label1.setLayoutData(gridData8);
		button = new Button(botComposite, SWT.NONE);
		button.setText("Run");
		button.setLayoutData(gridData9);
		button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				CodeModelCreator.createCodes("Demo1", tableInfos);
				  try {
						new ProgressMonitorDialog(sShell).run(true, true,
						        new CreateMasterOperation(tableInfos,combo.getText()));
					} catch (InvocationTargetException e1) {
						e1.printStackTrace();
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			}
		});
	}
	/**
	 * This method initializes combo	
	 *
	 */
	private void createCombo() {
		GridData gridData7 = new GridData();
		gridData7.grabExcessHorizontalSpace = true;
		combo = new Combo(topComposite, SWT.NONE);
		combo.setLayoutData(gridData7);
		
		String[] testArr = new String[CodeModelFactory.modelConfig.size()]; 
		
		int index=0;
		for(Map.Entry<String,List<Class>> ent :CodeModelFactory.modelConfig.entrySet())
		{
			testArr[index]=ent.getKey();
			index++;
		}
		   
		combo.setItems(testArr);  
		combo.setText(testArr[0]);  
	}
}
